// 导入数据库模块
const db = require('../DB/index');
// 导入生成token包
const jwt = require('jsonwebtoken');
const config = require('../config')


// 登入用户处理函数
exports.login =(req,res)=>{
    // 接受表单数据
    const userinfo = req.body
    // 定义SQL语句
    const sql = `select * from user where username=?`
    user = { ...userinfo }
    db.query(sql,[userinfo.username],(err,result)=>{

        // 执行sql语句失败
        if(err) return res.cc(err)
        // 获取数据条数不为一时，就是未查到该数据，
        if(result.length !== 1) return res.cc('登入失败，用户名输入错误')
        // 判断密码是否正确
        if(result[0].password !== userinfo.password) return res.cc('密码错误')
        // 对用户信息进行加密生成token
        const token = jwt.sign(user,config.jwtSecreKey, { expiresIn: config.expiresIn })
        // 返回token值
        res.send({
            status:0,
            message:'登入成功',
            role: result[0].role,
            token:'Bearer ' + token
        })
    })

}


// 通过 登入账号，姓名，所属区域，角色进行条件查询，输出查询数据
exports.getSearchInfo = (req,res)=>{
    // 从客户端传回来的数据
    const data = req.body
    let sql  = `select * from user where 1 = 1`
    let searchData = []
    //登陆账号、姓名、所属区域、角色
    if(data.username !== undefined && data.username !== ''){
        sql =sql + ` and username = ?`
        searchData.push(data.username)
    }
    if(data.name !== undefined && data.name !== ''){
        sql = sql +` adn name = ?`
        searchData.push(data.name)
    }
    if(data.region !== undefined && data.region !== ''){
        sql =sql+ ` and region = ?`
        searchData.push(data.region)
    }
    if(data.role !== undefined && data.role !== ''){
        sql =sql+ ` and role = ?`
        searchData.push(data.role)
    }
   db.query(sql,searchData,(err,result)=>{
    if(err) return res.cc(err)
    if(result.length === 0) return res.cc('该条件没有数值')
    res.send({
        status:0,
        message:'查询成功',
        data:result
    })
   })
}


// text
exports.text = (req, res) => {
    res.send({
        code: 200,
        data: [[
            { 
                title: 'Responsive Design',
                icon: 'view',
                info: 'Lorem ipsum dolor sit amet consectetur adipisicing elit. Iusto provident qui tempore natus quos quibusdam soluta at.'
            },
            { 
                title: 'Responsive Design',
                icon: 'money',
                info: 'Lorem ipsum dolor sit amet consectetur adipisicing elit. Iusto provident qui tempore natus quos quibusdam soluta at.'
            },
            { 
                title: 'Responsive Design',
                icon: 'order',
                info: 'Lorem ipsum dolor sit amet consectetur adipisicing elit. Iusto provident qui tempore natus quos quibusdam soluta at.'
            },
            { 
                title: 'Responsive Design',
                icon: 'menu',
                info: 'Lorem ipsum dolor sit amet consectetur adipisicing elit. Iusto provident qui tempore natus quos quibusdam soluta at.'
            },
            { 
                title: 'Responsive Design',
                icon: 'link',
                info: 'Lorem ipsum dolor sit amet consectetur adipisicing elit. Iusto provident qui tempore natus quos quibusdam soluta at.'
            },
            { 
                title: 'Responsive Design',
                icon: 'ip',
                info: 'Lorem ipsum dolor sit amasdfasdfasdfadsfasdfsafsdt consectetur adipisicing elit. Iusto provident qui tempore natus quos quibusdam soluta at.'
            }
        ]]
    })
}
